using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public partial class NodeTag : MonoBehaviour
{
    public void AddRigidbody2D()
    {
        Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
        if (rigidbody2D != null)
        {
            if (name.IndexOf("Wall") != -1) return;
            if (name.IndexOf("Lava") != -1) return;
            if (name.IndexOf("Spike") != -1) return;
            if (name.IndexOf("Water") != -1) return;
            n.RigidBody = new CRigidbody2D();
            n.RigidBody.Add = true;
            if (name.IndexOf("Dog") != -1)
            {
                n.RigidBody.AwakeOnLoad = false;
                n.RigidBody.Bullet = true;
            }
            if (name.IndexOf("spider") != -1 || name.IndexOf("fixed") != -1)
            {
                n.RigidBody.LinearDamping = 10;
                n.RigidBody.FixedRotation = true;
            }
            if(name.IndexOf("stone")!=-1)
            {
                n.RigidBody.Bullet = true;
                n.RigidBody.GravityScale = 1.5f;
                n.RigidBody.AwakeOnLoad = false;
            }
            if(name.IndexOf("balloon") !=-1)
            {
                n.RigidBody.GravityScale = -1.6f;
                n.RigidBody.AwakeOnLoad = true;
            }
        }
    }
    public void AddPolygonCollider2D()
    {
        PolygonCollider2D polygonCollider2D = GetComponent<PolygonCollider2D>();
        if (polygonCollider2D != null && (polygonCollider2D.enabled || IsAddNoEnable))
        {
            n.PhysicsPolygonCollider = new List<CPolygonCollider2D>();
            CPolygonCollider2D a = new CPolygonCollider2D();
            a.Add = true;
            n.PhysicsPolygonCollider.Add(a);
        }
    }
    public void AddCircleCollider2D()
    {
        CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
        RectTransform rect = GetComponent<RectTransform>();
        if (circleCollider2D != null && (circleCollider2D.enabled || IsAddNoEnable))
        {
            n.PhysicsCircleCollider = new CCircleCollider2D();
            n.PhysicsCircleCollider.Add = true;
            if (circleCollider2D.radius <= 0.5 && rect!=null)
            {
                n.PhysicsCircleCollider.Radius = rect.sizeDelta.x / 2;
            }
            else
            {
                n.PhysicsCircleCollider.Radius = circleCollider2D.radius;
            }
            if (name.IndexOf("Cellular") != -1)
            {
                n.PhysicsCircleCollider.Radius = 60;
                n.PhysicsCircleCollider.Tag = 5;
                n.PhysicsCircleCollider.Sensor = true;
                n.PhysicsCircleCollider.Friction = 0.2f;
            }
            if (name.IndexOf("Dog") != -1)
            {
                n.PhysicsCircleCollider.Radius = 60;
                n.PhysicsCircleCollider.Tag = 1;
                n.PhysicsCircleCollider.Sensor = false;
                n.PhysicsCircleCollider.Friction = 0.1f;
                n.PhysicsCircleCollider.Density = 1;
                n.PhysicsCircleCollider.Restitution = 0.5f;
            }
            if (name.IndexOf("spider") != -1)
            {
                n.PhysicsCircleCollider.Tag = 9;
                n.PhysicsCircleCollider.Radius = 50f;
                n.PhysicsCircleCollider.OffSet = new Vector2(0, -40);
            }
            if (name.IndexOf("fixed") != -1)
            {
                n.PhysicsCircleCollider.Tag = 9;
                n.PhysicsCircleCollider.Radius = 50f;
                n.PhysicsCircleCollider.Enabled = false;
            }
            if(name.IndexOf("balloon") !=-1)
            {
                n.PhysicsCircleCollider.Tag = 16;
                n.PhysicsCircleCollider.Radius = 55f;
                n.PhysicsCircleCollider.OffSet = new Vector2(0,10);
            }
            if(name.IndexOf("stone")!=-1)
            {
                n.PhysicsCircleCollider.Tag = 17;
                n.PhysicsCircleCollider.Radius = 60;
            }
           
        }
    }
    public void AddDistanceJoint2D()
    {
        DistanceJoint2D[] distanceJoint2DList = GetComponents<DistanceJoint2D>();
        if (distanceJoint2DList != null && distanceJoint2DList.Length > 0)
        {
            n.DistanceJoint = new List<CDistanceJoint2D>();
            for (int i = 0; i < distanceJoint2DList.Length && (distanceJoint2DList[i].enabled || IsAddNoEnable); i++)
            {
                CDistanceJoint2D d = new CDistanceJoint2D();
                d.Add = true;
                d.Anchor = distanceJoint2DList[i].anchor;
                n.DistanceJoint.Add(d);
            }
        }
    }
    public void AddRevoluteJoint()
    {
        if(name.IndexOf("spider")!=-1)
        {
            n.RevoluteJoint = new CRevoluteJoint();
            n.RevoluteJoint.Add = true;
            n.RevoluteJoint.Anchor = new Vector2(0, -26.2f);
            n.RevoluteJoint.ConnectedAnchor = new Vector2(0, 104.9f);
        }
    }
    public void AddPrismaticJoint()
    {
        if (name.IndexOf("spider") != -1)
        {
            n.PrismaticJoint = new CPrismaticJoint();
            n.PrismaticJoint.Add = true;
            n.PrismaticJoint.Enabled = false;
        }
    }
    public void AddBoxCollider2D()
    {
        BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
        if(name.IndexOf("Glass")!=-1 && boxCollider2D) {

            n.RigidBody = new CRigidbody2D();
            n.RigidBody.Add = true;
            n.BoxCollider = new CBoxCollider();
            n.BoxCollider.Add = true;
            n.BoxCollider.Friction = 0;
            n.BoxCollider.Tag = 102;
            n.BoxCollider.Offset = Vector2.zero;
            n.BoxCollider.Size = ConvertToCanvasPos(boxCollider2D.size);
        }
    }
    public void AddRopeJoint()
    {
        if(name.IndexOf("balloon") !=-1)
        {
            n.RopeJoint = new CRopeJoint();
            n.RopeJoint.Add = true;
            n.RopeJoint.Anchor = new Vector2(0, -65);
            n.RopeJoint.ConnectedAnchor = new Vector2(0, 100);
        }
    }
}
